package com.iretailer.report.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.ReQuartTask;
import com.iretailer.report.mapper.QuartTaskMapper;
import com.iretailer.report.quartz.QuartzService;
import com.iretailer.report.quartz.model.QuartTask;
import com.iretailer.report.util.SpringUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Auther Bennie
 * @Date 2020/7/1 10:44
 * @Description
 */

@Service
@Slf4j
public class QuartTaskService extends ServiceImpl<QuartTaskMapper, QuartTask> {
    @Autowired
    private QuartTaskMapper quartTaskMapper;

    @Autowired
    protected ModelMapper modelMapper;

    public PageResult<QuartTask> selectPage(String companyId, String jobGroup, String jobName, QueryOption queryOption) {
        Page page = new Page<QuartTask>(1, Long.MAX_VALUE);
        QueryWrapper<QuartTask> queryWrapper = new QueryWrapper<>();
        if (companyId != null) {
            queryWrapper.eq("companyId", companyId);
        }
        if (jobGroup != null) {
            queryWrapper.like("jobGroup", jobGroup);
        }
        if (jobName != null) {
            queryWrapper.like("jobName", jobName);
        }
        queryOption.judge(page, queryWrapper);
        IPage pageInfo = quartTaskMapper.selectPage(page, queryWrapper);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getRecords());
    }

    public boolean saveOrUpdate(QuartTask quartTask) {
        if (quartTask.getId() != null && quartTask.getId() > 0) {
            quartTaskMapper.updateById(quartTask);
        } else {
            quartTaskMapper.insert(quartTask);
        }
        return true;
    }

    @SneakyThrows
    @Transactional
    public boolean saveOrUpdateTask(ReQuartTask reQuartTask) {
        QuartzService quartzService = SpringUtils.getBean(QuartzService.class);
        Long id = reQuartTask.getId();
        boolean update = id != null;
        QuartTask quartTask = modelMapper.map(reQuartTask, QuartTask.class);
        QuartTask olderQuartTask = null;
        if (update) {
            olderQuartTask = getById(id);
        }
        boolean result = saveOrUpdate(quartTask);
        if (result) {
            if (update) {
                QuartTask task = getById(id);
                if (!task.getJobName().equals(olderQuartTask.getJobName()) || !task.getJobGroup().equals(olderQuartTask.getJobGroup())) {
                    quartzService.deleteJob(olderQuartTask);
                }
            }
            if (quartTask.getStatus() == 1) {
                quartzService.addJob(quartTask);
            } else {
                quartzService.deleteJob(quartTask);
            }
        }
        return true;
    }
}
